﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Presentation.Contracts;
using VIA.Presentation;
using VIA.Integration.Presentation;
using VIA.SiMoJi.Domain.Electronics;
using VIA.SiMoJi.Presentation.View;
using VIA.SiMoJi.Domain.Inspection;
using VIA.Project.Presentation.ViewModel.Inspection;
using VIA.SiMoJi.Domain.Option;
using VIA.Foundation;
using System.Windows.Documents;
using VIA.Integration.Domain;
using VIA.Integration.Modbus;
using System.Threading;
using VHslComm.ModBus;
using System.IO.Ports;
using NPOI.OpenXmlFormats.Shared;
using VIA.Integration.LeadShine;
using System.Diagnostics;

namespace VIA.SiMoJi.Presentation.ViewModel
{
    [ViewModel(typeof(IBanYunStationOptionView))]
    internal class BanYunStationOptionViewModel : EditorViewModel<IBanYunStationOptionView, BanYunStationOption>
    {
        private CylinderDebugViewModel cM_无杆缸;
        private CylinderDebugViewModel cM_定位缸;
        ModbusRtuPLC _modbus;
        SerialPort _serialPort;
        public BanYunStation BanYunStation { get; }
        public NozzleDebugViewModel NM_Nozzle1 { get; set; }
        public NozzleDebugViewModel NM_Nozzle2 { get; set; }
        public NozzleDebugViewModel NM_Nozzle3 { get; set; }
        public NozzleDebugViewModel NM_Nozzle4 { get; set; }
        public NozzleDebugViewModel NM_Nozzle5 { get; set; }
        public NozzleDebugViewModel NM_Nozzle6 { get; set; }
        public NozzleDebugViewModel NM_Nozzle7 { get; set; }
        public IRelayCommand ReadCommand { get; set; }
        public IRelayCommand SetPressCommand { get; set; }
        public IRelayCommand SetDefaultPressCommand { get; set; }
        private double press1;
        private double press2;
        private double press3;
        private double press4;
        private double press5;
        private double press6;
        private double press7;
        private double axispress1;
        private double axispress2;
        private double axispress3;
        private double axispress4;
        private double axispress5;
        private double axispress6;
        private double axispress7;
        private string address = "2";
        private int selectedAxis;
        private double currentIntValue;
        private double currentPress;

        public double Press1 { get => press1; set => SetProperty(ref press1, value); }
        public double Press2 { get => press2; set => SetProperty(ref press2, value); }
        public double Press3 { get => press3; set => SetProperty(ref press3, value); }
        public double Press4 { get => press4; set => SetProperty(ref press4, value); }
        public double Press5 { get => press5; set => SetProperty(ref press5, value); }
        public double Press6 { get => press6; set => SetProperty(ref press6, value); }
        public double Press7 { get => press7; set => SetProperty(ref press7, value); }
        public double AxisPress1 { get => axispress1; set => SetProperty(ref axispress1, value); }
        public double AxisPress2 { get => axispress2; set => SetProperty(ref axispress2, value); }
        public double AxisPress3 { get => axispress3; set => SetProperty(ref axispress3, value); }
        public double AxisPress4 { get => axispress4; set => SetProperty(ref axispress4, value); }
        public double AxisPress5 { get => axispress5; set => SetProperty(ref axispress5, value); }
        public double AxisPress6 { get => axispress6; set => SetProperty(ref axispress6, value); }
        public double AxisPress7 { get => axispress7; set => SetProperty(ref axispress7, value); }

        public double CurrentIntValue { get => currentIntValue; set => SetProperty(ref currentIntValue, value); }
        public double CurrentPress { get => currentPress; set => SetProperty(ref currentPress, value); }
        public string Address { get => address; set => SetProperty(ref address, value); }

        public int AxisId => SelectedAxis - 1;
        public int SelectedAxis { get => selectedAxis; set => SetProperty(ref selectedAxis, value); }

        public CylinderDebugViewModel CM_无杆缸 { get => cM_无杆缸; set => SetProperty(ref cM_无杆缸, value); }
        public CylinderDebugViewModel CM_定位缸 { get => cM_定位缸; set => SetProperty(ref cM_定位缸, value); }

        public IRelayCommand Read2Command { get; }

        public BanYunStationOptionViewModel(IServiceProvider service, Inspector inspector) : base(service)
        {
            BanYunStation = inspector.BanYunStation;
            _modbus = inspector.Modbus;
            _serialPort = inspector.SerialPort;
            _serialPort.DataReceived += SerialPort_DataReceived;
            NM_Nozzle1 = new(BanYunStation.N_夹爪1吸嘴);
            NM_Nozzle2 = new(BanYunStation.N_夹爪2吸嘴);
            NM_Nozzle3 = new(BanYunStation.N_夹爪3吸嘴);
            NM_Nozzle4 = new(BanYunStation.N_夹爪4吸嘴);
            NM_Nozzle5 = new(BanYunStation.N_夹爪5吸嘴);
            NM_Nozzle6 = new(BanYunStation.N_夹爪6吸嘴);
            NM_Nozzle7 = new(BanYunStation.N_夹爪7吸嘴);
            CM_定位缸 = new(BanYunStation.C_定位缸) { Name = "抽屉定位缸" };
            CM_无杆缸 = new(BanYunStation.C_无杆缸) { Name = "无杆缸" };
            ReadCommand = _commandFactory.Create(Read);
            SetPressCommand = _commandFactory.Create(SetPress);
            SetDefaultPressCommand = _commandFactory.Create(SetDefaultPress);
            Read2Command = _commandFactory.Create(Read2);
        }

        async Task Read2()
        {
            await Task.Run(() =>
            {
                var avglist = BanYunStation.CalcLiJuAvg();
                Debug.WriteLine(string.Join("|", avglist));
            });
        }

        private void SetDefaultPress()
        {
            try
            {
                //int data = 0;
                //Electronic.Controller.GetNodeOd(0, 2, 1018, 0x6077, 0, 16, ref data);
                //Electronic.Controller.SetNodeOd(0, 2, 1018, 0x6077, 0, 16, 0);
                //Electronic.Controller.GetNodeOd(0, 2, 1018, 0x6077, 0, 16, ref data);
                Electronic.Controller.SetNodeOd(0, 2, 1010, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1012, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1014, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1016, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1018, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1020, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
                Electronic.Controller.SetNodeOd(0, 2, 1022, 24690, 0, 16, (int)(Option.AxisPress * Option.K + Option.B));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        private void SetPress()
        {
            int pressIntValue = 1875;//初始设定值1875
            pressIntValue = (int)(Option.AxisPress * Option.K + Option.B);
            if (pressIntValue > 3500) pressIntValue = 3500;
            ushort nodeNum = (ushort)(1010 + AxisId * 2);
            Electronic.Controller.SetNodeOd(0, 2, nodeNum, 24690, 0, 16, pressIntValue);
            int value = 0;
            Electronic.Controller.GetNodeOd(0, 2, nodeNum, 24690, 0, 16, ref value);
        }

        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] buffer = new byte[1024];
                _serialPort.Read(buffer, 0, buffer.Length);
                byte[] data = new byte[4] { buffer[6], buffer[5], buffer[4], buffer[3] };
                if (buffer[0] != 1 || buffer[1] != 4) return;
                var press = BitConverter.ToSingle(data, 0);
                View.Dispatcher.Invoke(new Action(() =>
                {
                    if (SelectedAxis == 1)
                    {
                        Press1 = press;
                    }
                    else if (SelectedAxis == 2)
                    {
                        Press2 = press;
                    }
                    else if (SelectedAxis == 3)
                    {
                        Press3 = press;
                    }
                    else if (SelectedAxis == 4)
                    {
                        Press4 = press;
                    }
                    else if (SelectedAxis == 5)
                    {
                        Press5 = press;
                    }
                    else if (SelectedAxis == 6)
                    {
                        Press6 = press;
                    }
                    else if (SelectedAxis == 7)
                    {
                        Press7 = press;
                    }
                }));
            }
            catch (Exception ex)
            {
            }
        }

        public void Read()
        {
            try
            {
                if (!_serialPort.IsOpen)
                    _serialPort.Open();
                byte[] buffer = new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0xCB };
                _serialPort.Write(buffer, 0, buffer.Length);
                double k = Option.K;
                short value = 0;
                ushort nodeNum = (ushort)(1010 + AxisId * 2);
                Electronic.Controller.GetNodeOd(0, 2, nodeNum, 0x6077, 0, 16, ref value);
                CurrentIntValue = value;
                var press = (value - Option.B) / k;
                if (SelectedAxis == 1)
                {
                    AxisPress1 = press;
                }
                else if (SelectedAxis == 2)
                {
                    AxisPress2 = press;
                }
                else if (SelectedAxis == 3)
                {
                    AxisPress3 = press;
                }
                else if (SelectedAxis == 4)
                {
                    AxisPress4 = press;
                }
                else if (SelectedAxis == 5)
                {
                    AxisPress5 = press;
                }
                else if (SelectedAxis == 6)
                {
                    AxisPress6 = press;
                }
                else if (SelectedAxis == 7)
                {
                    AxisPress7 = press;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
    }
}
